package com.daisuxia.web.spider;

import java.io.File;
import java.io.IOException;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import com.daisuxia.constant.Constant;

/**
 * 获取html元素、值工具类
 * 
 * @author simalin
 * 
 */
public class ParseImpl {

	private static Logger loger = Logger.getLogger(ParseImpl.class);

	/**
	 * 通过url获取Document
	 * 
	 * @param url
	 * @return
	 */
	public static Document getDocmentByUrl(String url) {
		try {
			return Jsoup
					.connect(url)
					.userAgent(
							"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.31 (KHTML, like Gecko) Chrome/26.0.1410.64 Safari/537.31")
					.get();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * 模拟登录然后获取doc
	 * 
	 * @param url
	 * @param cookieName
	 * @param cookieCon
	 * @return
	 */
	public static Document getDocmentByUrlForLogin(String url,
			String cookieName, String cookieCon) {
		try {
			return Jsoup.connect(url).cookie(cookieName, cookieCon).get();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * 通过文件获取Document
	 * 
	 * @param fpath
	 * @param enCode
	 * @return
	 */
	public static Document getDocument(String fpath, String enCode) {
		File htmls = new File(fpath);
		try {
			Document doc = Jsoup.parse(htmls, enCode);
			return doc;
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * 通过tag获取elements
	 * 
	 * @param doc
	 * @param infoValue
	 * @return
	 */
	public static Elements getElementByTag(Document doc, String infoValue) {
		return doc.getElementsByTag(infoValue);
	}

	/**
	 * 根据属性及标签获取elements
	 * 
	 * @param doc
	 * @param labSelect
	 * @param infoKey
	 * @param infoValue
	 * @return
	 */
	public static Elements getElements(Document doc, String labSelect,
			String infoKey, String infoValue) {
		Elements eles = null;
		if (Constant.SCHEME_CLASS.equals(infoKey)) {
			eles = getElementsByClass(doc, labSelect, infoKey, infoValue);
		} else if (Constant.SCHEME_ID.equals(infoKey)) {
			eles = getElementsById(doc, labSelect, infoKey, infoValue);
		} else if (Constant.SCHEME_SELECT.equals(infoKey)) {

		} else if (null == infoKey) {
			if (null == labSelect && infoValue == null) {
				eles = getElementsByAll(doc);
			}
		}
		return eles;
	}

	/**
	 * 根据class获取elements
	 * 
	 * @param doc
	 * @param labSelect
	 * @param infoKey
	 * @param infoValue
	 * @return
	 */
	public static Elements getElementsByClass(Document doc, String labSelect,
			String infoKey, String infoValue) {
		Elements eles = null;
		if (null != doc) {
			eles = doc.getElementsByClass(infoValue).first().children();
		}
		if (StringUtils.isNotBlank(labSelect)) {
			return getElementByLabSelect(eles.first(), labSelect).children();
		} else {
			return eles;
		}
	}

	/**
	 * 根据id获取elements
	 * 
	 * @param doc
	 * @param labSelect
	 * @param infoKey
	 * @param infoValue
	 * @return
	 */
	public static Elements getElementsById(Document doc, String labSelect,
			String infoKey, String infoValue) {
		Elements eles = null;
		if (null != doc) {
			eles = doc.getElementById(infoValue).children();
		}
		if (StringUtils.isNotBlank(labSelect)) {
			return getElementByLabSelect(eles.first(), labSelect).children();
		} else {
			return eles;
		}
	}

	/**
	 * 获取doc下所有的element
	 * 
	 * @param doc
	 * @return
	 */
	public static Elements getElementsByAll(Document doc) {
		if (null != doc) {
			return doc.getAllElements();
		}
		return null;
	}

	/**
	 * 根据属性及标签获取element
	 * 
	 * @param doc
	 * @param labSelect
	 * @param infoKey
	 * @param infoValue
	 * @return
	 */
	public static Element getElement(Document doc, String labSelect,
			String infoKey, String infoValue) {
		Element element = null;
		if (null == doc) {

		} else {
			if (Constant.SCHEME_CLASS.equals(infoKey)) {
				element = getElementByClass(doc, labSelect, infoKey, infoValue);
				return element;
			} else if (Constant.SCHEME_ID.equals(infoKey)) {
				element = getElementById(doc, labSelect, infoKey, infoValue);
				return element;
			}
		}
		return null;
	}

	/**
	 * 根据id获取element
	 * 
	 * @param doc
	 * @param labSelect
	 * @param infoKey
	 * @param infoValue
	 * @return
	 */
	public static Element getElementById(Document doc, String labSelect,
			String infoKey, String infoValue) {
		Element element = null;
		if (StringUtils.isBlank(labSelect)) {// 选择为null
			element = doc.getElementById(infoValue);
			return element;
		} else {
			element = getElementByLabSelect(doc.getElementById(infoValue),
					labSelect);
			return element;
		}
	}

	/**
	 * 根据class获取element
	 * 
	 * @param doc
	 * @param labSelect
	 * @param infoKey
	 * @param infoValue
	 * @return
	 */
	public static Element getElementByClass(Document doc, String labSelect,
			String infoKey, String infoValue) {
		Element element = null;
		if (StringUtils.isBlank(labSelect)) {
			element = doc.getElementsByClass(infoValue).first();
			return element;
		} else {
			element = getElementByLabSelect(doc.getElementsByClass(infoValue)
					.first(), labSelect);
			return element;
		}
	}

	/**
	 * 通过选择标签获取element
	 * 
	 * @param ele
	 * @param labSelect
	 * @return
	 */
	public static Element getElementByLabSelect(Element ele, String labSelect) {
		Element element = null;
		element = ele.select(labSelect).first();
		return element;
	}

	/**
	 * 通过elements选择获取element
	 * 
	 * @param eles
	 * @param labSelect
	 * @return
	 */
	public static Element getElementByLabSelect(Elements eles, String labSelect) {
		Element element = null;
		element = eles.select(labSelect).first();
		return element;
	}

	/**
	 * 获取属性值
	 * 
	 * @param ele
	 * @param infoKey
	 * @return
	 */
	public static String getAttributes(Element ele, String infoKey) {
		// loger.info("getAttributes:ele="+ele+" infoKey="+infoKey);
		String obj = null;
		if (StringUtils.isNotBlank(infoKey)) {
			String[] keyArray = infoKey.split(",");// 当出现属性时可以多种选择
			if (null != keyArray) {
				for (int i = 0; i < keyArray.length; i++) {
					if (StringUtils.isNotBlank(keyArray[i])
							&& StringUtils.isNotEmpty(keyArray[i])) {
						obj = ele.attributes().get(keyArray[i]);
					}
					if (StringUtils.isNotBlank(obj)
							&& StringUtils.isNotEmpty(obj)) {
						return obj;
					}
				}
			}
		}
		return obj;
	}

	/**
	 * 获取html及text内容
	 * 
	 * @param ele
	 * @param infoKey
	 * @return
	 */
	public static String getTextAndHtml(Element ele, String infoKey) {
		if (Constant.SCHEME_TEXT.equals(infoKey)) {
			return ele.text();
		} else if (Constant.SCHEME_OWNTEXT.equals(infoKey)) {
			return ele.ownText();
		} else {
			return ele.html();
		}

	}

	/**
	 * 获取结果
	 * 
	 * @param ele
	 * @param labSelect
	 * @param infoKey
	 * @param infoValue
	 * @return
	 */
	public static String getVaule(Element ele, String labSelect,
			String infoKey, String infoValue) {
		loger.info("ele=" + ele + " labSelect=" + labSelect + "   infoKey="
				+ infoKey + "   infoValue=" + infoValue);
		// loger.info("labSelect="+labSelect+"   infoKey="+infoKey+"   infoValue="+infoValue);

		String obj = null;
		if (StringUtils.isBlank(labSelect)) {// 当前标签
			if (Constant.SCHEME_ATTR.equals(infoKey)) {
				obj = getAttributes(ele, infoValue);
			} else if (Constant.SCHEME_TEXT.equals(infoKey)
					|| Constant.SCHEME_HTML.equals(infoKey)
					|| Constant.SCHEME_OWNTEXT.equals(infoKey)) {
				if (StringUtils.isBlank(infoValue)) {
					obj = getTextAndHtml(ele, infoKey);
				} else {

				}
			}
		} else {// 需要选择的标签
			if (Constant.SCHEME_ATTR.equals(infoKey)) {
				Element el = getElementByLabSelect(ele, labSelect);
				if (null == el) {
					return null;
				}
				obj = getAttributes(el, infoValue);
			} else if (Constant.SCHEME_TEXT.equals(infoKey)
					|| Constant.SCHEME_HTML.equals(infoKey)
					|| Constant.SCHEME_OWNTEXT.equals(infoKey)) {
				Element el = getElementByLabSelect(ele, labSelect);
				if (null == el) {
					return null;
				}
				obj = getTextAndHtml(el, infoKey);
			}
		}
		return obj;
	}

}
